package com.lanou3g.Hw0514.View;

import com.lanou3g.Hw0514.controller.Controller;
import com.lanou3g.Hw0514.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password =request.getParameter("password");
        Controller controller = new Controller();
        User user= controller.login(username,password);
        String msg ="";
        if (user==null){
            msg="用户名/密码错误";
        }else {
            msg="登录成功";
        }
        //向request域中放值
        //值以key-value的形式存放
        //key只能是String Value可以是任何对象
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("/msg").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
